//
//    Selection.swift
//
//    Create by 徐挺 陈 on 29/3/2018
//    Copyright © 2018. All rights reserved.
//    Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import Foundation
import SwiftyJSON
import PromiseKit


class Selection : NSObject, NSCoding{
    
    var selectText : String!
    var selectType : String!
    var selectValue : Int!
    
    
    /**
     * Instantiate the instance using the passed json values to set the properties values
     */
    init(fromJson json: JSON!){
        if json.isEmpty{
            return
        }
        selectText = json["selectText"].stringValue
        selectType = json["selectType"].stringValue
        selectValue = json["selectValue"].intValue
    }
    
    /**
     * Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
     */
    func toDictionary() -> [String:Any]
    {
        var dictionary = [String:Any]()
        if selectText != nil{
            dictionary["selectText"] = selectText
        }
        if selectType != nil{
            dictionary["selectType"] = selectType
        }
        if selectValue != nil{
            dictionary["selectValue"] = selectValue
        }
        return dictionary
    }
    
    /**
     * NSCoding required initializer.
     * Fills the data from the passed decoder
     */
    @objc required init(coder aDecoder: NSCoder)
    {
        selectText = aDecoder.decodeObject(forKey: "selectText") as? String
        selectType = aDecoder.decodeObject(forKey: "selectType") as? String
        selectValue = aDecoder.decodeObject(forKey: "selectValue") as? Int
        
    }
    
    /**
     * NSCoding required method.
     * Encodes mode properties into the decoder
     */
    func encode(with aCoder: NSCoder)
    {
        if selectText != nil{
            aCoder.encode(selectText, forKey: "selectText")
        }
        if selectType != nil{
            aCoder.encode(selectType, forKey: "selectType")
        }
        if selectValue != nil{
            aCoder.encode(selectValue, forKey: "selectValue")
        }
        
    }

    class func getList() -> Promise<[Selection]>{
        var arr: [Selection] = Array()
        let dataFn = { (json: JSON) -> Void in
            let _ = json.array?.map({
                arr.append(Selection(fromJson: $0))
            })
        }
        let request = RequestData(path: "Main/GetSelection")
        return netRequest(request, dataFn: dataFn).then { _ in
            return Promise(value: arr)
        }
    }
}
